<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>MVC 应用（MVC Applications） &mdash; Phalcon 2.0.0 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework"><link rel="stylesheet" type="text/css" href="../_static/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="http://static.phalconphp.com/css/phalcon.min.css" />
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="http://fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.0.0',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="../_static/jquery.min.js"></script>
    <script type="text/javascript" src="../_static/docs.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Phalcon 2.0.0 文档" href="../index.html" />
    <link rel="next" title="路由（Routing）" href="routing.html" />
    <link rel="prev" title="Volt 模版引擎（Volt: Template Engine）" href="volt.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="http://phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="http://blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <!--<div class="header-line">
      <div class="size-wrap">
        <div class="header-line-title title-white">Documentation</div>
      </div>
    </div>-->
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="routing.html" title="路由（Routing）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="volt.html" title="Volt 模版引擎（Volt: Template Engine）"
             accesskey="P">上一页</a> |</li>
        <li><a href="http://phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 2.0.0 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script type="text/javascript">var z = document.createElement("script"); z.type = "text/javascript"; z.async = true; z.src = "http://engine.carbonads.com/z/56496/azcarbon_2_1_0_VERT"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s);</script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">MVC 应用（MVC Applications）</a><ul>
<li><a class="reference internal" href="#single-or-multi-module-applications">单模块或多模块应用（Single or Multi Module Applications）</a><ul>
<li><a class="reference internal" href="#single-module">单模块（Single Module）</a></li>
<li><a class="reference internal" href="#multi-module">多模块（Multi Module）</a></li>
</ul>
</li>
<li><a class="reference internal" href="#understanding-the-default-behavior">理解默认行为（Understanding the default behavior）</a></li>
<li><a class="reference internal" href="#manual-bootstrapping">手动启动（Manual bootstrapping）</a></li>
<li><a class="reference internal" href="#application-events">应用事件（Application Events）</a></li>
<li><a class="reference internal" href="#external-resources">外部资源（External Resources）</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="volt.html" title="上一章">&lt; Volt 模版引擎（Volt: Template Engine）</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="routing.html" title="下一章">路由（Routing） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/applications.txt" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="mvc-mvc-applications">
<h1>MVC 应用（MVC Applications）<a class="headerlink" href="#mvc-mvc-applications" title="永久链接至标题">¶</a></h1>
<p>在Phalcon，策划MVC操作背后的全部困难工作通常都可以
通过 <a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> 做到。这个组件封装了全部后端所需要的复杂
操作，实例化每一个需要用到的组件并与项目整合在一起，从而使得MVC模式可以如期地运行。</p>
<div class="section" id="single-or-multi-module-applications">
<h2>单模块或多模块应用（Single or Multi Module Applications）<a class="headerlink" href="#single-or-multi-module-applications" title="永久链接至标题">¶</a></h2>
<p>通过这个组件，你可以运行各式各样的MVC结构：</p>
<div class="section" id="single-module">
<h3>单模块（Single Module）<a class="headerlink" href="#single-module" title="永久链接至标题">¶</a></h3>
<p>单一的MVC应用仅仅包含了一个模块。可以使用命名空间，但不是必需的。
这样类型的应用可能会有以下文件目录结构：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">single/</span>
<span class="x">    app/</span>
<span class="x">        controllers/</span>
<span class="x">        models/</span>
<span class="x">        views/</span>
<span class="x">    public/</span>
<span class="x">        css/</span>
<span class="x">        img/</span>
<span class="x">        js/</span>
</pre></div>
</div>
<p>如果未使用命名空间，以下的启动文件可用于编排MVC工作流：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Loader</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Application</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\DI\FactoryDefault</span><span class="p">;</span>

<span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Loader</span><span class="p">();</span>

<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerDirs</span><span class="p">(</span>
    <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;../apps/controllers/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;../apps/models/&#39;</span>
    <span class="p">)</span>
<span class="p">)</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>

<span class="nv">$di</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FactoryDefault</span><span class="p">();</span>

<span class="c1">// 注册视图组件</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>
    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../apps/views/&#39;</span><span class="p">);</span>
    <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
<span class="p">});</span>

<span class="k">try</span> <span class="p">{</span>

    <span class="nv">$application</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Application</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>

    <span class="k">echo</span> <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>

<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>如果使用了命名空间，则可以使用以下启动文件（译者注：主要区别在于使用$loader的方式）：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Loader</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Dispatcher</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Application</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\DI\FactoryDefault</span><span class="p">;</span>


<span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Loader</span><span class="p">();</span>

<span class="c1">// 根据命名空间前缀加载</span>
<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerNamespaces</span><span class="p">(</span>
    <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;Single\Controllers&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;../apps/controllers/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;Single\Models&#39;</span>      <span class="o">=&gt;</span> <span class="s1">&#39;../apps/models/&#39;</span><span class="p">,</span>
    <span class="p">)</span>
<span class="p">)</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>

<span class="nv">$di</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FactoryDefault</span><span class="p">();</span>

<span class="c1">// 注册调度器，并设置控制器的默认命名空间</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;dispatcher&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nv">$dispatcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Dispatcher</span><span class="p">();</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setDefaultNamespace</span><span class="p">(</span><span class="s1">&#39;Single\Controllers&#39;</span><span class="p">);</span>
    <span class="k">return</span> <span class="nv">$dispatcher</span><span class="p">;</span>
<span class="p">});</span>

<span class="c1">// 注册视图组件</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>
    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../apps/views/&#39;</span><span class="p">);</span>
    <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
<span class="p">});</span>

<span class="k">try</span> <span class="p">{</span>

    <span class="nv">$application</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Application</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>

    <span class="k">echo</span> <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>

<span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">){</span>
    <span class="k">echo</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="multi-module">
<h3>多模块（Multi Module）<a class="headerlink" href="#multi-module" title="永久链接至标题">¶</a></h3>
<p>多模块的应用使用了相同的文档根目录但拥有多个模块。在这种情况下，可以使用以下的文件目录结构：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">multiple/</span>
<span class="x">  apps/</span>
<span class="x">    frontend/</span>
<span class="x">       controllers/</span>
<span class="x">       models/</span>
<span class="x">       views/</span>
<span class="x">       Module.php</span>
<span class="x">    backend/</span>
<span class="x">       controllers/</span>
<span class="x">       models/</span>
<span class="x">       views/</span>
<span class="x">       Module.php</span>
<span class="x">  public/</span>
<span class="x">    css/</span>
<span class="x">    img/</span>
<span class="x">    js/</span>
</pre></div>
</div>
<p>在apps/下的每一个目录都有自己的MVC结构。Module.php文件代表了各个模块不同的配置，如自动加载器和自定义服务：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">namespace</span> <span class="nx">Multiple\Backend</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Phalcon\Loader</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Dispatcher</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\ModuleDefinitionInterface</span><span class="p">;</span>


<span class="k">class</span> <span class="nc">Module</span> <span class="k">implements</span> <span class="nx">ModuleDefinitionInterface</span>
<span class="p">{</span>

    <span class="sd">/**</span>
<span class="sd">     * 注册自定义加载器</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">registerAutoloaders</span><span class="p">()</span>
    <span class="p">{</span>

        <span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Loader</span><span class="p">();</span>

        <span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerNamespaces</span><span class="p">(</span>
            <span class="k">array</span><span class="p">(</span>
                <span class="s1">&#39;Multiple\Backend\Controllers&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;../apps/backend/controllers/&#39;</span><span class="p">,</span>
                <span class="s1">&#39;Multiple\Backend\Models&#39;</span>      <span class="o">=&gt;</span> <span class="s1">&#39;../apps/backend/models/&#39;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">);</span>

        <span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="sd">/**</span>
<span class="sd">     * 注册自定义服务</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">registerServices</span><span class="p">(</span><span class="nv">$di</span><span class="p">)</span>
    <span class="p">{</span>

        <span class="c1">//Registering a dispatcher</span>
        <span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;dispatcher&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>
            <span class="nv">$dispatcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Dispatcher</span><span class="p">();</span>
            <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setDefaultNamespace</span><span class="p">(</span><span class="s2">&quot;Multiple\Backend\Controllers&quot;</span><span class="p">);</span>
            <span class="k">return</span> <span class="nv">$dispatcher</span><span class="p">;</span>
        <span class="p">});</span>

        <span class="c1">//Registering the view component</span>
        <span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>
            <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>
            <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../apps/backend/views/&#39;</span><span class="p">);</span>
            <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
        <span class="p">});</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>还需要一个指定的启动文件来加载多模块的MVC架构：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Router</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Application</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\DI\FactoryDefault</span><span class="p">;</span>


<span class="nv">$di</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FactoryDefault</span><span class="p">();</span>

<span class="c1">// 自定义路由</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;router&#39;</span><span class="p">,</span> <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>

    <span class="nv">$router</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Router</span><span class="p">();</span>

    <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">setDefaultModule</span><span class="p">(</span><span class="s2">&quot;frontend&quot;</span><span class="p">);</span>

    <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">add</span><span class="p">(</span><span class="s2">&quot;/login&quot;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;module&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;backend&#39;</span><span class="p">,</span>
        <span class="s1">&#39;controller&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;login&#39;</span><span class="p">,</span>
        <span class="s1">&#39;action&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;index&#39;</span><span class="p">,</span>
    <span class="p">));</span>

    <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">add</span><span class="p">(</span><span class="s2">&quot;/admin/products/:action&quot;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;module&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;backend&#39;</span><span class="p">,</span>
        <span class="s1">&#39;controller&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;products&#39;</span><span class="p">,</span>
        <span class="s1">&#39;action&#39;</span>     <span class="o">=&gt;</span> <span class="mi">1</span><span class="p">,</span>
    <span class="p">));</span>

    <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">add</span><span class="p">(</span><span class="s2">&quot;/products/:action&quot;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;controller&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;products&#39;</span><span class="p">,</span>
        <span class="s1">&#39;action&#39;</span>     <span class="o">=&gt;</span> <span class="mi">1</span><span class="p">,</span>
    <span class="p">));</span>

    <span class="k">return</span> <span class="nv">$router</span><span class="p">;</span>
<span class="p">});</span>

<span class="k">try</span> <span class="p">{</span>

    <span class="c1">// 创建应用</span>
    <span class="nv">$application</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Application</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>

    <span class="c1">// 注册模块</span>
    <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">registerModules</span><span class="p">(</span>
        <span class="k">array</span><span class="p">(</span>
            <span class="s1">&#39;frontend&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
                <span class="s1">&#39;className&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Multiple\Frontend\Module&#39;</span><span class="p">,</span>
                <span class="s1">&#39;path&#39;</span>      <span class="o">=&gt;</span> <span class="s1">&#39;../apps/frontend/Module.php&#39;</span><span class="p">,</span>
            <span class="p">),</span>
            <span class="s1">&#39;backend&#39;</span>  <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
                <span class="s1">&#39;className&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Multiple\Backend\Module&#39;</span><span class="p">,</span>
                <span class="s1">&#39;path&#39;</span>      <span class="o">=&gt;</span> <span class="s1">&#39;../apps/backend/Module.php&#39;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">)</span>
    <span class="p">);</span>

    <span class="c1">// 处理请求</span>
    <span class="k">echo</span> <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>

<span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">){</span>
    <span class="k">echo</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>如果你想在启动文件保持模块的配置，你可以使用匿名函数来注册对应的模块：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>

<span class="c1">// 创建视图组件</span>
<span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>

<span class="c1">// 设置视图组件相关选项</span>
<span class="c1">//...</span>

<span class="c1">// Register the installed modules</span>
<span class="nv">$application</span><span class="o">-&gt;</span><span class="na">registerModules</span><span class="p">(</span>
    <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;frontend&#39;</span> <span class="o">=&gt;</span> <span class="k">function</span><span class="p">(</span><span class="nv">$di</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$di</span><span class="o">-&gt;</span><span class="na">setShared</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
                <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../apps/frontend/views/&#39;</span><span class="p">);</span>
                <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
            <span class="p">});</span>
        <span class="p">},</span>
        <span class="s1">&#39;backend&#39;</span> <span class="o">=&gt;</span> <span class="k">function</span><span class="p">(</span><span class="nv">$di</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$di</span><span class="o">-&gt;</span><span class="na">setShared</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
                <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../apps/backend/views/&#39;</span><span class="p">);</span>
                <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
            <span class="p">});</span>
        <span class="p">}</span>
    <span class="p">)</span>
<span class="p">);</span>
</pre></div>
</div>
<p>当 <a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> 有多个模块注册时，通常
每个都是需要的，以便每一个被匹配到的路由都能返回一个有效的模块。每个已经注册的模块都有一个相关的类来提供建立和启动自身的函数。
而每个模块定义的类都必须实现registerAutoloaders()和registerServices()这两个方法，这两个函数会在模块即被执行时被</p>
<blockquote>
<div><a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> 调用。</div></blockquote>
</div>
</div>
<div class="section" id="understanding-the-default-behavior">
<h2>理解默认行为（Understanding the default behavior）<a class="headerlink" href="#understanding-the-default-behavior" title="永久链接至标题">¶</a></h2>
<p>如果你已经看过了 <a class="reference internal" href="tutorial.html"><em>tutorial</em></a> 或者已经通过 <a class="reference internal" href="tools.html"><em>Phalcon Devtools</em></a> 生成了代码，
你将很容易识别以下的启动文件：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">try</span> <span class="p">{</span>

    <span class="c1">// 注册自动加载器</span>
    <span class="c1">//...</span>

    <span class="c1">// 注册服务</span>
    <span class="c1">//...</span>

    <span class="c1">// 处理请求</span>
    <span class="nv">$application</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Phalcon\Mvc\Application</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>

    <span class="k">echo</span> <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>

<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="s2">&quot;Exception: &quot;</span><span class="p">,</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>控制器中全部核心的工作都会在handle()被回调时触发执行。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">echo</span> <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="manual-bootstrapping">
<h2>手动启动（Manual bootstrapping）<a class="headerlink" href="#manual-bootstrapping" title="永久链接至标题">¶</a></h2>
<p>如果你不想使用 <a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> ，以上的代码可以改成这样：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// 获取 &#39;router&#39; 服务</span>
<span class="nv">$router</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;router&#39;</span><span class="p">];</span>

<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">();</span>

<span class="nv">$view</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;view&#39;</span><span class="p">];</span>

<span class="nv">$dispatcher</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;dispatcher&#39;</span><span class="p">];</span>

<span class="c1">// 传递路由的相关数据传递给调度器</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setControllerName</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getControllerName</span><span class="p">());</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setActionName</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getActionName</span><span class="p">());</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setParams</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getParams</span><span class="p">());</span>

<span class="c1">// 启动视图</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">start</span><span class="p">();</span>

<span class="c1">// 请求调度</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">dispatch</span><span class="p">();</span>

<span class="c1">// 渲染相关视图</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">getControllerName</span><span class="p">(),</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">getActionName</span><span class="p">(),</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">getParams</span><span class="p">()</span>
<span class="p">);</span>

<span class="c1">// 完成视图</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">finish</span><span class="p">();</span>

<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;response&#39;</span><span class="p">];</span>

<span class="c1">// 传递视图内容给响应对象</span>
<span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setContent</span><span class="p">(</span><span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">());</span>

<span class="c1">// 发送头信息</span>
<span class="nv">$response</span><span class="o">-&gt;</span><span class="na">sendHeaders</span><span class="p">();</span>

<span class="c1">// 输出响应内容</span>
<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>
</pre></div>
</div>
<p>以下代码替换了 <a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> ，虽然缺少了视图组件，
但却更适合Rest风格的API接口：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// 获取 &#39;router&#39; 服务</span>
<span class="nv">$router</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;router&#39;</span><span class="p">];</span>

<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">();</span>

<span class="nv">$dispatcher</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;dispatcher&#39;</span><span class="p">];</span>

<span class="c1">// 传递路由的相关数据传递给调度器</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setControllerName</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getControllerName</span><span class="p">());</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setActionName</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getActionName</span><span class="p">());</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setParams</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getParams</span><span class="p">());</span>

<span class="c1">// 请求调度</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">dispatch</span><span class="p">();</span>

<span class="c1">// 获取最后的返回结果</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">getReturnedValue</span><span class="p">();</span>

<span class="c1">// 判断结果是否是 &#39;response&#39; 对象</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$response</span> <span class="nx">instanceof</span> <span class="nx">Phalcon\Http\ResponseInterface</span><span class="p">)</span> <span class="p">{</span>

    <span class="c1">// 发送响应</span>
    <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>另外一个修改就是在分发器中对抛出异常的捕捉可以将请求转发到其他的操作：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// 获取 &#39;router&#39; 服务</span>
<span class="nv">$router</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;router&#39;</span><span class="p">];</span>

<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">();</span>

<span class="nv">$dispatcher</span> <span class="o">=</span> <span class="nv">$di</span><span class="p">[</span><span class="s1">&#39;dispatcher&#39;</span><span class="p">];</span>

<span class="c1">// 传递路由的相关数据传递给调度器</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setControllerName</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getControllerName</span><span class="p">());</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setActionName</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getActionName</span><span class="p">());</span>
<span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setParams</span><span class="p">(</span><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">getParams</span><span class="p">());</span>

<span class="k">try</span> <span class="p">{</span>

    <span class="c1">// 请求调度</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">dispatch</span><span class="p">();</span>

<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>

    <span class="c1">//An exception has occurred, dispatch some controller/action aimed for that</span>

    <span class="c1">// Pass the processed router parameters to the dispatcher</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setControllerName</span><span class="p">(</span><span class="s1">&#39;errors&#39;</span><span class="p">);</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">setActionName</span><span class="p">(</span><span class="s1">&#39;action503&#39;</span><span class="p">);</span>

    <span class="c1">// Dispatch the request</span>
    <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">dispatch</span><span class="p">();</span>

<span class="p">}</span>

<span class="c1">// 获取最后的返回结果</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$dispatcher</span><span class="o">-&gt;</span><span class="na">getReturnedValue</span><span class="p">();</span>

<span class="c1">// 判断结果是否是 &#39;response&#39; 对象</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$response</span> <span class="nx">instanceof</span> <span class="nx">Phalcon\Http\ResponseInterface</span><span class="p">)</span> <span class="p">{</span>

    <span class="c1">// 发送响应</span>
    <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>尽管上面的代码比使用 <a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> 而需要的代码远远要累赘得很，
但它为启动你的应用提供了一个可修改、可定制化的途径。
因为根据你的项目需要，你可以想对实例什么和不实例化什么进行完全的控制，或者想用你自己的组件来替代那些确定和必须的组件从而扩展默认的功能。</p>
</div>
<div class="section" id="application-events">
<h2>应用事件（Application Events）<a class="headerlink" href="#application-events" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> 可以把事件发送到 <a class="reference internal" href="events.html"><em>EventsManager</em></a> （如果它激活的话）。
事件将被当作&#8221;application&#8221;类型被消费掉。目前已支持的事件如下：</p>
<table border="1" class="docutils">
<colgroup>
<col width="25%" />
<col width="75%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">事件名称</th>
<th class="head">消费于</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>boot</td>
<td>当应用处理它首个请求时被执行</td>
</tr>
<tr class="row-odd"><td>beforeStartModule</td>
<td>在初始化模块之前，仅当模块被注册时</td>
</tr>
<tr class="row-even"><td>afterStartModule</td>
<td>在初始化模块之后，仅当模块被注册时</td>
</tr>
<tr class="row-odd"><td>beforeHandleRequest</td>
<td>在执行分发环前</td>
</tr>
<tr class="row-even"><td>afterHandleRequest</td>
<td>在执行分发环后</td>
</tr>
</tbody>
</table>
<p>以下示例演示了如何将侦听器绑定到组件：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Events\Manager</span> <span class="k">as</span> <span class="nx">EventsManager</span><span class="p">;</span>

<span class="nv">$eventsManager</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventsManager</span><span class="p">();</span>

<span class="nv">$application</span><span class="o">-&gt;</span><span class="na">setEventsManager</span><span class="p">(</span><span class="nv">$eventsManager</span><span class="p">);</span>

<span class="nv">$eventsManager</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span>
    <span class="s2">&quot;application&quot;</span><span class="p">,</span>
    <span class="k">function</span><span class="p">(</span><span class="nv">$event</span><span class="p">,</span> <span class="nv">$application</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// ...</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="external-resources">
<h2>外部资源（External Resources）<a class="headerlink" href="#external-resources" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="https://github.com/phalcon/mvc">Github上的MVC示例</a></li>
</ul>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="routing.html" title="路由（Routing）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="volt.html" title="Volt 模版引擎（Volt: Template Engine）"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
  <div class="container">
      <div class="row">
          <div class="col-xs-4 col-sm-3">
              <h4>Download</h4>
              <ul>
                  <li><a href="http://phalconphp.com/download">Installing Phalcon</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://api.phalconphp.com">API</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-3">
              <h4>Community</h4>
              <ul>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                  <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                  <li><a href="http://phalconphp.com/en/testimonials">Testimonials</a></li>
                  <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                  <li><a href="http://store.phalconphp.com/">Store</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-2">
              <h4>About</h4>
              <ul>
                  <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                  <li><a href="http://phalconphp.com/en/team">Team</a></li>
                  <li><a href="http://phalconphp.com/en/roadmap">Roadmap</a></li>
                  <li><a href="http://phalconphp.com/en/donate">Donate</a></li>
                  <li><a href="http://phalconphp.com/en/consulting">Consulting</a></li>
                  <li><a href="http://phalconphp.com/en/hosting">Hosting</a></li>
              </ul>
          </div>
          <div id="license-spaccer" class="visible-xs"></div>
          <div id="license-wrapper" class="col-xs-12 col-sm-4">
              <p class="license">

                  Found a typo or an error? Want to improve this document? The documentation sources are available on <a href="http://github.com/phalcon/docs">Github</a><br>
                  Need support or have questions? Check our <a href="http://forum.phalconphp.com">Forum</a><br>
                  <br>

                  Phalcon Framework is released under the <a href="https://github.com/phalcon/cphalcon/blob/master/docs/LICENSE.md">new BSD license</a>.<br>
                  Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>

                <div class="design">
                  <span>Designed by:</span>

                  <a href="http://www.fog-city.net/" class="fogcity" target="_blank" title="Fog City Software"><span>Fog City Software</span></a>
              </div>
          </div>
      </div>
  </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>